﻿using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using QuickOpen.Framework.Items;

namespace QuickOpen.Framework.Comparers
{
    public class FileComparer : IComparer<File>
    {
        /// <summary>
        /// Create a new instance of the FileComparer passing in the appropriate sort type.
        /// </summary>
        /// <param name="type">The sort type to use.</param>
        public FileComparer(FileCompareType type)
        {
            _type = type;
        }

        #region IComparer<File> Members

        /// <summary>
        /// Compares to <see cref="File"/> references.
        /// </summary>
        /// <param name="x">The first file</param>
        /// <param name="y">The second file</param>
        /// <returns>-1 if x comes before y, 0 if the two are equal, 1 if x comes after y</returns>
        public int Compare(File x, File y)
        {
            //TODO: Lookup project types to determine which extension to check for. Will reduce conditional checks.

            if (
                _type == FileCompareType.CodePrefered
                && (x.FileNameWithoutExtension.Equals(y.FileNameWithoutExtension, StringComparison.CurrentCultureIgnoreCase))
                && ((x.FileName.EndsWith(".cs") ^ y.FileName.EndsWith(".cs")) ||
                (x.FileName.EndsWith(".vb") ^ y.FileName.EndsWith(".vb")) ||
                (x.FileName.EndsWith(".cpp") ^ y.FileName.EndsWith(".cpp")))
                )
            {
                if (x.FileName.EndsWith(".cs") || x.FileName.EndsWith(".vb") || x.FileName.EndsWith(".cpp"))
                    return -1;
                else
                    return 1;
            } else
                return x.FileName.CompareTo(y.FileName);
        }

        #endregion

        private FileCompareType _type;
    }
}
